/**
 * 
 */
package com.sx.feesunit.action;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ModelDriven;
import com.sx.core.BaseAction;
import com.sx.core.JSONUtils;
import com.sx.feesunit.biz.IAreaBiz;
import com.sx.feesunit.biz.IUserUnitBiz;
import com.sx.feesunit.entity.AreaEntity;
import com.sx.feesunit.entity.UserUnitEntity;
import com.sx.feesunit.exception.SXException;

/**
 * @author Administrator
 *
 */
@SuppressWarnings("serial")
public class UserUnitAction extends BaseAction implements ModelDriven<UserUnitEntity>{
	private IUserUnitBiz iuserunitbiz;
	private UserUnitEntity userunit = new UserUnitEntity();
	private Log log = LogFactory.getLog(this.getClass());
	private File userinfoexcel;
	private String userinfoexcelFileName;
	/**
	 * importUserInfo
	 * 导入用户数据信息
	 **/
	public void importUserInfo() throws SXException{
		try {
			List<UserUnitEntity> list = iuserunitbiz.getUserUnitInfoFormExcel(userinfoexcel, userinfoexcelFileName);
            
			outJsonString("{success:true}");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			log.error("UserUnit查询And失败出错",e);
			outJsonString("{success:false}");
			throw new SXException(e);
		}
	}
	/**
	 * findUserUnitInfoIsOwed
	 * 查询用户没有出错信息不能进行分页
	 **/
	public void findUserUnitInfoIsOwed() throws SXException{
		String str = "[]";
		try {
			List<UserUnitEntity> list = new ArrayList<UserUnitEntity>();
			list = iuserunitbiz.findUserUnitInfoIsOwed(userunit);
			str = JSONArray.toJSONString(list);
			outJsonString(str);
		} catch (Exception e) {
			log.error("UserUnit查询And失败出错",e);
			outJsonString(str);
			throw new SXException(e);
		}
	}
	/**
	 * findUserUnitNoPaging
	 * 查询用户信息不能进行分页
	 **/
	public void findUserUnitNoPaging() throws SXException{
		String str = "[]";
		try {
			List<UserUnitEntity> list = new ArrayList<UserUnitEntity>();
			list = iuserunitbiz.findUserUnitInfo(userunit);
			str = JSONArray.toJSONString(list);
			outJsonString(str);
		} catch (Exception e) {
			log.error("UserUnit查询And失败出错",e);
			outJsonString(str);
			throw new SXException(e);
		}
	}
	/**
	 * 删除用户信息
	 **/
	public void deleteUserUnitInfo() throws SXException{
		try {
			boolean flag = iuserunitbiz.deleteUserUnitInfo(userunit);
			if (flag) {
				outJsonString("{success:true}");
			} else {
				outJsonString("{success:false}");
			}
		} catch (Exception e) {
			log.error("删除用户信息出错",e);
			super.outJsonString("{success:false}");
			throw new SXException(e);
			// TODO: handle exception
		}
	}
	/**
	 * 修改用户信息
	 **/
	public void editUserUnitInfo() throws SXException{
		try {
			boolean flag = iuserunitbiz.updateUserUnitInfo(userunit);
			if (flag) {
				outJsonString("{success:true}");
			} else {
				outJsonString("{success:false}");
			}
		} catch (Exception e) {
			log.error("修改用户信息出错",e);
			super.outJsonString("{success:false}");
			throw new SXException(e);
			// TODO: handle exception
		}
	}
	/**
	 *获取全部的用户数据信息
	 **/
	public void findUserUnitByAndCondiction() throws SXException{
		String str = "{totalCount:0,data:[]}";
		try {
			List<UserUnitEntity> list = new ArrayList<UserUnitEntity>();
			list = iuserunitbiz.findUserUnitInfoByAndCondiction(userunit);
			str = JSONUtils.convertListToJson(UserUnitEntity.class, list, list.get(0).getTotal());
			outJsonString(str);
		} catch (Exception e) {
			log.error("UserUnit查询And失败出错",e);
			outJsonString(str);
			throw new SXException(e);
		}
	}
	/**
	 * 新增UserUnit数据信息
	 **/
	public void addUserUnitInfo()throws SXException{
		try {
			Integer id = iuserunitbiz.addUserUnitInfo(userunit);
			if (id.equals(0)) {
				outJsonString("{success:false,uid:0}");
			} else {
				outJsonString("{success:true,uid:"+id.intValue()+"}");
			}
		} catch (Exception e) {
			log.error("新增用户出错",e);
			super.outJsonString("{success:false,uid:0}");
			throw new SXException(e);
			// TODO: handle exception
		}
	}
	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */
	public UserUnitEntity getModel() {
		return userunit;
	}
	/**
	 * @return the iuserunitbiz
	 */
	public IUserUnitBiz getIuserunitbiz() {
		return iuserunitbiz;
	}
	/**
	 * @param iuserunitbiz the iuserunitbiz to set
	 */
	public void setIuserunitbiz(IUserUnitBiz iuserunitbiz) {
		this.iuserunitbiz = iuserunitbiz;
	}
	/**
	 * @return the userinfoexcel
	 */
	public File getUserinfoexcel() {
		return userinfoexcel;
	}
	/**
	 * @param userinfoexcel the userinfoexcel to set
	 */
	public void setUserinfoexcel(File userinfoexcel) {
		this.userinfoexcel = userinfoexcel;
	}
	/**
	 * @return the userinfoexcelFileName
	 */
	public String getUserinfoexcelFileName() {
		return userinfoexcelFileName;
	}
	/**
	 * @param userinfoexcelFileName the userinfoexcelFileName to set
	 */
	public void setUserinfoexcelFileName(String userinfoexcelFileName) {
		this.userinfoexcelFileName = userinfoexcelFileName;
	}
	
}
